/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager;

import br.com.foxline.clinicplus.entity.Doctor;
import br.com.foxline.clinicplus.service.SystemUserService;
import java.io.IOException;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

/**
 *
 * @author roniere
 */
@ManagedBean
@SessionScoped
public class ManagerLayout implements Serializable {

    @EJB
    SystemUserService systemUserService;
    String page;
    private Doctor doctor;

    @PostConstruct
    public void init() {
        getDoctorByLogin();
    }

    public String addPage(String type) {
        return "indexImovel.xhtml";
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }


    public boolean getDoctorByLogin() {
        String user = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();
        doctor = systemUserService.getDoctorByLogin(user);
        if (doctor == null) {
            return false;
        } else {
            return true;
        }
    }

    public void logout() throws IOException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
        String contextName = servletContext.getContextPath();
        facesContext.responseComplete();
        facesContext.getExternalContext().invalidateSession();
        facesContext.getExternalContext().redirect("/ClinicPlus-war/login.jsp");
//        return "login.jsp";
    }

    public void showUser() throws IOException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
        String contextName = servletContext.getContextPath();
        facesContext.getExternalContext().redirect("/ClinicPlus-war/showuser.xhtml?userShow=" + systemUserService.getCurrentUser().getId());

    }
    
    public Long idUser(){
        return systemUserService.getCurrentUser().getId();
    }

 

    public Doctor getDoctor() {
        return doctor;
    }
}
